<template>
  <div class="app-container">
    管理员姓名： <el-input style="width: 200px"
              placeholder="请输入内容"
              prefix-icon="el-icon-search"
              v-model="sheltersName">
    </el-input>
    管理员账号： <el-input style="width: 200px"
              placeholder="请输入内容"
              prefix-icon="el-icon-search"
              v-model="sheltersAccount">
    </el-input>
    管辖方舱： <el-select v-model="sheltersId2"
               clearable
               placeholder="请选择方舱">
      <el-option v-for="item in sheltersList"
                 :key="item.sheltersId"
                 :label="item.sheltersName"
                 :value="item.sheltersId">
      </el-option>
    </el-select>
    <el-row style="margin:10px"> 注册时间：
      <el-date-picker v-model="begin"
                      type="date"
                      value-format="yyyy-MM-dd"
                      placeholder="选择日期">
      </el-date-picker>
      - <el-date-picker v-model="end"
                      type="date"
                      value-format="yyyy-MM-dd"
                      placeholder="选择日期">
      </el-date-picker>
      <el-button type="primary"
                 style="margin: 10px"
                 @click="selectShelters()">搜索</el-button>
      <el-button type="primary"
                 style="margin: 10px"
                 @click="resetForm()">重置</el-button>
      <el-button type="primary"
                 @click="dialogFormVisible = true">添加</el-button>
    </el-row>
    <el-table v-loading="listLoading"
              :data="list"
              element-loading-text="Loading"
              border
              fit
              highlight-current-row>
      <el-table-column type="selection"
                       width="55">
      </el-table-column>
      <el-table-column align="center"
                       label="ID"
                       width="95">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="管辖方舱"
                       width=auto>
        <template slot-scope="scope">
          {{ scope.row.shelters.sheltersName }}
        </template>
      </el-table-column>
      <el-table-column label="管理员姓名"
                       width=auto
                       align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.userName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系方式"
                       width="110"
                       align="center">
        <template slot-scope="scope">
          {{ scope.row.userPhone }}
        </template>
      </el-table-column>
      <el-table-column label="管理员账号"
                       width=auto
                       align="center">
        <template slot-scope="scope">
          {{ scope.row.account }}
        </template>
      </el-table-column>
      <el-table-column label="密码"
                       width=auto
                       align="center">
        <template slot-scope="scope">
          {{ scope.row.userPassword }}
        </template>
      </el-table-column>
      <el-table-column align="center"
                       prop="created_at"
                       label="注册时间"
                       width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ new Date(scope.row.registerDate).toLocaleDateString() }}</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col"
                       label="操作"
                       width=auto
                       align="center">
        <template slot-scope="scope">
          <el-button type="success"
                     plain
                     @click="editor(scope.row)">编辑</el-button>
          <el-button type="danger"
                     plain
                     @click="deleteShelters(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加方舱管理员"
               :visible.sync="dialogFormVisible">
      <el-form :model="shelters">
        <el-form-item label="管理员姓名"
                      :label-width="formLabelWidth">
          <el-input v-model="shelters.userName"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="管辖方舱"
                      :label-width="formLabelWidth">
          <el-select v-model="sheltersId"
                     placeholder="请选择方舱">
            <el-option v-for="item in sheltersList"
                       :key="item.sheltersId"
                       :label="item.sheltersName"
                       :value="item.sheltersId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系方式"
                      :label-width="formLabelWidth">
          <el-input v-model="shelters.userPhone"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="管理员账号"
                      :label-width="formLabelWidth">
          <el-input v-model="shelters.account"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码"
                      :label-width="formLabelWidth">
          <el-input v-model="shelters.userPassword"
                    autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="insertShelters()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改区审核员信息"
               :visible.sync="dialogForm1Visible">
      <el-form :model="shelters1">
        <el-form-item label="管理员姓名"
                      :label-width="form1LabelWidth">
          <el-input v-model="shelters1.userName"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="管辖方舱"
                      :label-width="form1LabelWidth">
          <el-select v-model="sheltersId1"
                     placeholder="请选择方舱">
            <el-option v-for="(item,index) in sheltersList"
                       :key="index"
                       :label="item.sheltersName"
                       :value="item.sheltersId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系方式"
                      :label-width="form1LabelWidth">
          <el-input v-model="shelters1.userPhone"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="管理员账号"
                      :label-width="form1LabelWidth">
          <el-input v-model="shelters1.account"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码"
                      :label-width="form1LabelWidth">
          <el-input v-model="shelters1.userPassword"
                    autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogForm1Visible = false">取 消</el-button>
        <el-button type="primary"
                   @click="updateShelters()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分页插件 -->
    <div class="block"
         style="float:right">
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page.sync="pageIndex"
                     :page-sizes="pageSizes"
                     :page-size.sync="pageSize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="Total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getShelters, getSheltersList, insertShelters } from '@/api/preAdmin/centerAdmin'
import { deleteShelters, updateShelters } from '@/api/preAdmin/centerAdmin'

export default {
  filters: {
    statusFilter (status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data () {
    return {
      list: null,
      sheltersList: [],
      listLoading: true,
      sheltersName: null,
      sheltersAccount: null,
      sheltersId: null,
      sheltersId1: "",
      sheltersId2: null,
      begin: null,
      end: null,
      //分页
      currentPage: 1,
      Total: 0,
      pageIndex: 1,
      pageSize: 5,
      pageSizes: [5, 10, 15, 20],
      sheltersIdList: "",
      newName: "",
      dialogFormVisible: false,
      dialogForm1Visible: false,
      shelters: {
        // userName: '',
        // areaId: '',
        // userPhone: '',
        // account: '',
        // userPassword: ''
        userName: "",
        account: "",
        userPassword: "",
        userPhone: "",
        roleId: ""
      },
      shelters1: {
        userId: "",
        userName: "",
        account: "",
        userPassword: "",
        userPhone: "",
        roleId: ""
      },
      formLabelWidth: '120px',
      form1LabelWidth: '120px'
    }
  },
  created () {
    this.fetchData(),
      // this.getRegionList()
      this.getSheltersList()
  },
  methods: {
    fetchData () {
      this.listLoading = true
      getShelters(this.pageIndex, this.pageSize).then(response => {
        this.list = response.obj.list
        localStorage.setItem("list", JSON.stringify(this.list))
        this.Total = response.obj.total
        this.listLoading = false
      })
    },
    getSheltersList () {
      getSheltersList().then(response => {
        this.sheltersList = response.list
        // console.log(this.sheltersList)
      })
    },
    // 查询管理员
    selectShelters () {
      if (this.sheltersName == "" && this.sheltersAccount == "" && this.sheltersId2 == "" && this.begin == "" && this.end == "") {
        alert("查询条件不能为空！")
        getShelters(this.pageIndex, this.pageSize).then(response => {
          this.list = response.obj.list
          this.Total = response.obj.total
          this.listLoading = false
        })
      }
      getShelters(this.pageIndex, this.pageSize, this.sheltersName, this.sheltersAccount, this.sheltersId2, this.begin, this.end).then(response => {
        this.list = response.obj.list
        this.Total = response.obj.total
        this.listLoading = false
      })
    },
    //重置查询条件
    resetForm () {
      this.sheltersName = null
      this.sheltersAccount = null
      this.sheltersId2 = null
      this.begin = null
      this.end = null
      this.fetchData()
      // this.list = JSON.parse(localStorage.getItem("list"))
    },
    //增加管理员
    insertShelters () {
      if (this.sheltersId != "0") {
        this.shelters.roleId = "5"
      } else {
        this.shelters.roleId = "4"
      }
      insertShelters(this.shelters, this.sheltersId).then(response => {
        let message = response.msg
        this.$message({
          type: 'success',
          message: message
        })
        this.dialogFormVisible = false
        this.fetchData()
        this.shelters.userName = "",
          this.shelters.account = "",
          this.shelters.userPhone = "",
          this.shelters.userPassword = "",
          this.shelters.roleId = "",
          this.sheltersId = ""
      })
    },
    //编辑管理员
    editor (item) {
      this.dialogForm1Visible = true
      this.sheltersId1 = item.shelters.sheltersName
      this.shelters1 = item
      // console.log(item)
    },
    updateShelters () {
      this.sheltersIdList = this.sheltersId1
      // console.log(this.sheltersId1)
      console.log(this.sheltersIdList)
      if (this.sheltersId1 != "0") {
        this.shelters1.roleId = "5"
      } else {
        this.shelters1.roleId = "4"
      }
      updateShelters(this.sheltersIdList, this.shelters1.roleId, this.shelters1).then(response => {
        // console.log(this.areaIdList)
        let message = response.msg
        this.$message({
          type: 'success',
          message: message
        })
        this.dialogForm1Visible = false
        this.fetchData()
      })
    },
    //删除管理员
    deleteShelters (item) {
      console.log(item)
      deleteShelters(item.userId).then(response => {
        this.$message({
          message: response.msg,
          type: 'success'
        })
        this.fetchData()
      })
    },
    // 分页
    // 每页显示的条数
    handleSizeChange (val) {
      // 改变每页显示的条数 
      this.pageSize = val
      // this.pageSize=val
      // 点击每页显示的条数时，显示第一页
      // this.fetchData.getRegionCheck(1,val,1)
      // 在改变每页显示的条数时，要将页码显示到第一页
      // this.pageIndex=1
      this.fetchData()
    },
    // 显示第几页
    handleCurrentChange (val) {
      // 改变默认的页数
      this.pageIndex = val
      // 切换页码时，要获取每页显示的条数
      // this.pageSize=val
      // this.pageSize = (val)*(this.pageSize)
      this.fetchData()
    },
  }
}
</script>
